<template>
  <el-container class="container">
    <!-- 头部区域-->
    <el-header>
      <div>
        <img src="../assets/images/avater.jpg" alt="">
        <span>后台管理系统</span>
      </div>
      <el-button type="primary" @click="layout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <!-- 侧边栏的菜单 -->
        <div class="toggleBtn">
          <span class="el-icon-s-fold" @click="fold"></span>
          <span class="el-icon-s-unfold" @click="unfold"></span>
        </div>
        <!-- unique-opened是否只保持一个子菜单的展开 
        collapse	是否水平折叠收起菜单（仅在 mode 为 vertical 时可用）
        router是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转-->
        <el-menu background-color="#4a82c4" text-color="#fff" active-text-color="#d87093" :unique-opened="true"
          :collapse="collapseState" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <!-- 注意index绑定的值是字符串,不然会报错 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <!-- 图标 -->
              <i :class="icons[item.id]"></i>
              <!-- 文字 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <!-- 注意遍历的是item.children -->
            <el-menu-item-group v-for="smallItem in item.children" :key="smallItem.id">
              <!-- index 作为 path 进行路由跳转,前面补/ -->
              <el-menu-item :index="'/' + smallItem.path" @click="saveUrl('/' + smallItem.path)">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文字 -->
                <span>{{ smallItem.authName }}</span>

              </el-menu-item>

            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主要内容部分 -->
      <el-main>
        <!-- welcome组件展示部分 -->
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据对象
      menuList: [],
      // 图标对象
      icons: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      // 菜单栏是否折叠
      collapseState: false,
      // 激活的状态
      activePath: '',
    }
  },
  methods: {
    layout() {
      window.sessionStorage.clear();
      this.$router.push('/login');
    },
    // 获取菜单数据
    async getMenuData() {
      const { data: res } = await this.$http.get('menus');
      // console.log(res);
      if (res.meta.status !== 200) return this.$msg.error(res.meta.msg);
      this.menuList = res.data;
    },
    // toggleCollapse() {
    //   this.collapseState = !this.collapseState;
    // },
    // 控制菜单栏的折叠与否
    fold() {
      this.collapseState = true;
    },
    unfold() {
      this.collapseState = false;
    },
    // 保存激活的url地址
    saveUrl(activePath) {
      window.sessionStorage.setItem('activePath', activePath);
      this.activePath = activePath;//页面没刷新时
    }
  },
  created() {
    this.getMenuData();
    // 页面刷新时，将激活的url地址赋给activePath
    this.activePath = window.sessionStorage.getItem('activePath');
  },

}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}

.el-header {
  padding: 0;
  // 两端对齐
  display: flex;
  justify-content: space-between;
  // 使按钮居中
  align-items: center;
  background-color: #4a82c4;

  div {
    display: flex;
    align-items: center;

    img {
      width: 55px;
      height: 55px;
      margin-right: 10px;
      border-radius: 50%;
    }

    span {
      color: #fff;
      font-size: 20px;
    }
  }


}

.el-aside {
  background-color: #4a82c4;

  .toggleBtn {
    height: 30px;
    padding: 0 30px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: palegoldenrod;

    .el-icon-s-fold,
    .el-icon-s-unfold {
      width: 30px;
      height: 16px;
    }
  }

  .el-menu {
    // 解决菜单右端多出部分
    border-right: 0;
  }
}

.el-main {
  background-color: #e3fafa;
}

.iconfont {
  margin-right: 10px;
}
</style>